﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour 
{
    [SerializeField] AudioClip[] musicClips;
    [SerializeField] AudioClip[] audioClips;

    AudioSource musicSource;
    AudioSource uiSource;

    AudioWrapper musicWrapper = new AudioWrapper("music");
    AudioWrapper audioWrapper = new AudioWrapper("audio");

    Dictionary<string, AudioClip> musicDict = new Dictionary<string, AudioClip>();
    Dictionary<string, AudioClip> audioDict = new Dictionary<string, AudioClip>();

    private void Awake()
    {
        musicSource = gameObject.AddComponent<AudioSource>();
        uiSource = gameObject.AddComponent<AudioSource>();
        musicSource.loop = true;

        foreach (var item in musicClips)
        {
            if (musicDict.ContainsKey(item.name))
            {
                musicDict.Add(item.name, item);
            }
        }

        foreach (var item in audioClips)
        {
            if (audioDict.ContainsKey(item.name))
            {
                audioDict.Add(item.name, item);
            }
        }
        musicWrapper.onVolumeChanged =()=> musicSource.volume = musicWrapper.Volume;
        musicWrapper.onMuteChanged =()=> musicSource.mute = musicWrapper.Mute;
        audioWrapper.onVolumeChanged = () => uiSource.volume = audioWrapper.Volume;
        audioWrapper.onMuteChanged = () => uiSource.mute = audioWrapper.Mute;


        musicSource.volume = musicWrapper.Volume;
        musicSource.mute = musicWrapper.Mute;
        uiSource.volume = audioWrapper.Volume;
        uiSource.mute = audioWrapper.Mute;
    }

    public void PauseMusic()
    {
        musicSource.Pause();
    }

    public void StopMusic()
    {
        musicSource.Stop();
    }

    public float GetMusicVolume()
    {
        return musicWrapper.Volume;
    }
    public float GetAudioVolume()
    {
        return audioWrapper.Volume;
    }

    public void SetMusicMute(bool value)
    {
        musicWrapper.Mute = value;
    }

    public void SetAudioMute(bool value)
    {
        uiSource.mute = value;
    }

    public void SetMusicVolume(int value)
    {
        musicWrapper.Volume = value;
    }

    public void SetAudioVolume(int value)
    {
        audioWrapper.Volume = value;
    }

    public void PlayMusic(string name)
    {
        if (musicWrapper.Mute)
            return;

        if (!musicDict.ContainsKey(name))
            return;

        if (musicSource.clip != null && musicSource.clip.name != name)
        {
            musicSource.clip = musicDict[name];
        }

        musicSource.Play();
    }

    public void PlayUIAudio(string name)
    {
        if (audioWrapper.Mute)
            return;
        if (!audioDict.ContainsKey(name))
            return;

        uiSource.clip = audioDict[name];
        uiSource.Play();
    }

    public void PlayAudio(string name, Vector3 point)
    {
        if (audioWrapper.Mute)
            return;
        if (!audioDict.ContainsKey(name))
            return;

        AudioSource.PlayClipAtPoint(audioDict[name], point, GetAudioVolume() );
    }
}
